﻿Imports System.Windows.Forms
Imports Oracle.DataAccess.Client
Public Class ObjetosMN
    Private lDrObj As dsMenus.GLO_OBJETOSRow
    Private lEsPadre As Boolean
    Public Sub New(ByVal IdObjeto As Integer)
        Using conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Using cmd As OracleCommand = conn.CreateCommand
                cmd.CommandText = "SELECT * FROM GLO_OBJETOS WHERE IDOBJETO=:PIDOBJETO"
                cmd.Parameters.Add("PIDOBJETO", OracleDbType.Int32).Value = IdObjeto
                Try
                    Dim lDtObj As New dsMenus.GLO_OBJETOSDataTable
                    conn.Open()
                    lDtObj.Load(cmd.ExecuteReader)
                    conn.Close()
                    If lDtObj.Rows.Count > 0 Then
                        lDrObj = lDtObj.Rows(0)
                        lEsPadre = False
                    Else
                        lEsPadre = True
                    End If
                Catch ex As Exception
                    Throw ex
                End Try
            End Using
        End Using
    End Sub
    Public ReadOnly Property Espadre() As Boolean
        Get
            Return lEsPadre
        End Get
    End Property
    Public ReadOnly Property IdObjeto() As Integer
        Get
            Return lDrObj.IDOBJETO
        End Get
    End Property
    Public ReadOnly Property TipoObjeto() As Integer
        Get
            Return lDrObj.TIPOOBJETO
        End Get
    End Property
    Public ReadOnly Property NombreObjeto() As String
        Get
            Return lDrObj.NOMBREOBJETO
        End Get
    End Property
    Public ReadOnly Property Descripcion() As String
        Get
            Return lDrObj.DESCRIPCION
        End Get
    End Property
End Class
Public Class cMenu
    Private lDaMenu As dsMenusTableAdapters.GLO_MENUBYUSERTableAdapter
    Private lDtMenu As dsMenus.GLO_MENUBYUSERDataTable
    Private lTV As TreeView
    Private lListaImg As ImageList
    Private lUserGroup As String
    Private Sub CargaA_TV(ByVal IndicePadre As Integer, ByVal NodoPadre As TreeNode)
        Dim dvHijos As DataView = New DataView(lDtMenu)
        dvHijos.RowFilter = "IDPADRE=" + IndicePadre.ToString
        For Each drCurrent As DataRowView In dvHijos
            Dim NuevoNodo As New TreeNode
            NuevoNodo.Text = drCurrent("DESCRIPCION")
            Dim ObjMn As New ObjetosMN(drCurrent("IDOBJETO"))
            If Not ObjMn.Espadre Then
                NuevoNodo.Tag = ObjMn.IdObjeto
                NuevoNodo.ImageIndex = ObjMn.TipoObjeto + 1
                NuevoNodo.SelectedImageIndex = ObjMn.TipoObjeto + 1
                NuevoNodo.StateImageIndex = ObjMn.TipoObjeto + 1
            Else
                NuevoNodo.Tag = 0
                NuevoNodo.ImageIndex = 0
                NuevoNodo.SelectedImageIndex = 0
                NuevoNodo.StateImageIndex = 0
            End If
            If NodoPadre Is Nothing Then
                lTV.Nodes.Add(NuevoNodo)
            Else
                NodoPadre.Nodes.Add(NuevoNodo)
            End If
            CargaA_TV(drCurrent("IDNODO"), NuevoNodo)
        Next
    End Sub
    Private Sub CargaA_Dt(ByVal NodoPadre As TreeNode, ByVal Nivel As Integer)
        Dim drMenu As dsMenus.GLO_MENUBYUSERRow = lDtMenu.NewGLO_MENUBYUSERRow
        drMenu.USEROGROUP = lUserGroup
        drMenu.IDNODO = lDtMenu.Rows.Count + 1
        drMenu.IDPADRE = Nivel
        drMenu.IDOBJETO = NodoPadre.Tag
        drMenu.DESCRIPCION = NodoPadre.Text
        lDtMenu.Rows.Add(drMenu)
        drMenu = Nothing
        If NodoPadre.Nodes.Count > 0 Then
            Nivel = lDtMenu.Rows.Count
        End If
        For Each Nodo As TreeNode In NodoPadre.Nodes
            CargaA_Dt(Nodo, Nivel)
        Next
    End Sub
    Private Sub EliminaFilas()
        For i As Integer = lDtMenu.Rows.Count - 1 To 0 Step -1
            lDtMenu.Rows(i).Delete()
        Next
    End Sub
    Public Sub SalvaTV()
        Try
            EliminaFilas()
            lDaMenu.Update(lDtMenu)
            CargaA_Dt(lTV.Nodes(0), 0)
            lDaMenu.Update(lDtMenu)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub MezclarMenu(ByVal dtTem As dsMenus.GLO_MENUBYUSERDataTable)
        Dim Ultimo As Integer = 0
        If lDtMenu.Rows.Count > 0 Then _
            Ultimo = lDtMenu.Rows(lDtMenu.Rows.Count - 1)("IDNODO")
        Dim NodoActual As Integer = Ultimo + 1
        For Each drTem As dsMenus.GLO_MENUBYUSERRow In dtTem.Rows
            Dim drMn As dsMenus.GLO_MENUBYUSERRow = lDtMenu.NewGLO_MENUBYUSERRow
            drMn.DESCRIPCION = drTem.DESCRIPCION
            drMn.IDNODO = NodoActual
            NodoActual += 1
            drMn.IDPADRE = IIf(drTem.IDPADRE = 0, 0, drTem.IDPADRE + Ultimo)
            drMn.IDOBJETO = drTem.IDOBJETO
            drMn.USEROGROUP = drTem.USEROGROUP
            lDtMenu.Rows.Add(drMn)
            drMn = Nothing
        Next
    End Sub
    Public Sub New(ByVal User As String, ByVal mTV As TreeView, ByVal ImgLst As ImageList, ByVal All As Int16)
        Try
            lDaMenu = New dsMenusTableAdapters.GLO_MENUBYUSERTableAdapter
            lDtMenu = lDaMenu.GetDataByUser(User)
            For Each Grupo As String In CustomPrincipal.Roles.Split(",")
                Dim DtMn As dsMenus.GLO_MENUBYUSERDataTable = lDaMenu.GetDataByUser(Grupo)
                MezclarMenu(DtMn)
            Next
            lTV = mTV
            lTV.Nodes.Clear()
            lListaImg = ImgLst
            CargaA_TV(0, Nothing)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub New(ByVal UserGroup As String, ByVal mTV As TreeView, ByVal ImgLst As ImageList)
        Try
            lDaMenu = New dsMenusTableAdapters.GLO_MENUBYUSERTableAdapter
            lDtMenu = lDaMenu.GetDataByUser(UserGroup)
            lTV = mTV
            lTV.Nodes.Clear()
            lUserGroup = UserGroup
            lListaImg = ImgLst
            CargaA_TV(0, Nothing)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

End Class

